(define stream-car 
    (lambda (s) (car (force s)))
)
(define stream-cdr 
    (lambda (s) (cdr (force s)))
)
; chez-schema 无法用cons-stream,智能在内容写
; (define cons-stream
;     (lambda (a d) 
;         (delay (cons a d))
;     )
; )

(define (print-stream stream n)
    (define (print-item s count)
        (if (= count n)
            `done
            (begin
                (display (stream-car s))
                (display ",")
                (print-item (stream-cdr s) (+ count 1))
            )
        )
    )
    (newline)
    (display "s=")
    (print-item stream 0)
)

(define counters
    (let next ([n 1])
        (delay (cons n (next (+ n 1))))
    )
)

(define (scale-stream s x)
    (delay (cons (* (stream-car s) x) (scale-stream (stream-cdr s) x)))
)

(define (merge s1 s2)
    (cond 
        ((null? s1) s2)
        ((null? s2) s1)
        (else
            (let 
                ( 
                    (s1car (stream-car s1))
                    (s2car (stream-car s2))
                )
                (cond 
                    ((< s1car s2car)
                        (delay (cons s1car (merge (stream-cdr s1) s2)))
                    )
                    ((> s1car s2car)
                        (delay (cons s2car (merge s1 (stream-cdr s2))))
                    )
                    (else
                        (delay (cons s1car (merge (stream-cdr s1) (stream-cdr s2))))
                    )
                )
            )
        )
    )
)



(define s 
    (delay (cons 1 
        (merge
            (scale-stream s 2)
            (merge
                (scale-stream s 3)
                (scale-stream s 5)
            )
        )
    ))
)

(print-stream s 1)
(exit)